import json
from apps.merchant.item.dao import ItemDao
from apps.user.dao import UserDao
from apps.pay.recordIncomes.dao import RecordIncomesDao
from decimal import Decimal
from apps.system.lessee.dao import LesseeDao
def countFranchiseeAward(order):
    _order = order
    print('发放加盟店奖励', order.content)
    content = json.loads(_order.content)
    sellingPrice = order.sellingPrice
    discountsRate = Decimal(content.get('discountsRate'))
    franchiseeParams = json.loads(content.get('franchiseeParams'))
    shopParentId = content.get('shopParentId')
    shopUserId = content.get('shopUserId')
    # 赠送贡献值
    sendContributionValues = int(sellingPrice / int(franchiseeParams.get('selfSendContributionValueRate')))
    # 赠送购买者锁定积分
    selfSendIncomesLockeds = sellingPrice * Decimal(franchiseeParams.get('selfSendIncomesLockedRate'))
    # 加盟商让利
    discountsAward = sellingPrice * discountsRate
    # 加盟商余额
    shop_balance = sellingPrice - discountsAward
    # 直推购物积分
    verticalSendIncomes = discountsAward * Decimal(franchiseeParams.get('verticalSendIncomesRate'))
    # 日释放池
    freePoolDays = discountsAward * Decimal(franchiseeParams.get('dayAwardRate'))
    # 月释放池
    freePoolMonths = discountsAward * Decimal(franchiseeParams.get('monthAwardRate'))
    # 运营补贴
    operationAwards = discountsAward * Decimal(franchiseeParams.get('operationAwardRate'))
    # 商户推荐人奖励
    shopParentAward = discountsAward * Decimal(franchiseeParams.get('shopParentAwardRate'))
    # 助残基金
    helpFunds = discountsAward * Decimal(franchiseeParams.get('helpFundRate'))
    # 赠送加盟店锁定积分
    shopSendIncomesLockeds = discountsAward * Decimal(franchiseeParams.get('shopSendIncomesLockedRate'))
    # 毛利润
    grossProfits = discountsAward - (verticalSendIncomes + freePoolDays + freePoolMonths + operationAwards + shopParentAward + helpFunds)

    # 赠送贡献点
    payway = 'contributionValue'
    sellingPrice = _order.sellingPrice
    UserDao().addWallet(payway, _order.user, sendContributionValues)
    data = {}
    data['scene'] = 'sendForExpense'
    data['user'] = _order.user.id
    data['fromUser'] = _order.user.id
    data['orderId'] = _order.orderId
    data['money'] = sendContributionValues
    data['payway'] = payway
    data['changeWay'] = 'add'
    data['orderStatus'] = 'FINISH'
    RecordIncomesDao().create(data)

    # 赠送上级贡献点
    payway = 'contributionValue'
    if _order.user.parent:
        verticalSendContributionValues = int(sendContributionValues / 2)
        UserDao().addWallet(payway, _order.user.parent, verticalSendContributionValues)
        data = {}
        data['scene'] = 'verticalAward'
        data['user'] = _order.user.parent.id
        data['fromUser'] = _order.user.id
        data['orderId'] = _order.orderId
        data['money'] = verticalSendContributionValues
        data['payway'] = payway
        data['changeWay'] = 'add'
        data['orderStatus'] = 'FINISH'
        RecordIncomesDao().create(data)

    # 赠送锁定积分
    payway = 'incomesLocked'
    UserDao().addWallet(payway, _order.user, selfSendIncomesLockeds)
    data = {}
    data['scene'] = 'sendForExpense'
    data['user'] = _order.user.id
    data['fromUser'] = _order.user.id
    data['orderId'] = _order.orderId
    data['money'] = selfSendIncomesLockeds
    data['moneyLocked'] = selfSendIncomesLockeds
    data['payway'] = payway
    data['changeWay'] = 'add'
    data['orderStatus'] = 'FINISH'
    RecordIncomesDao().create(data)
    # 赠送商户锁定积分
    payway = 'incomesLocked'
    if shopUserId:
        shopUser = UserDao().findOneByObjId(shopUserId)
        UserDao().addWallet(payway, shopUser, shopSendIncomesLockeds)
        data = {}
        data['scene'] = 'sendForExpense'
        data['shop'] = _order.shop.id
        data['user'] = shopUserId
        data['fromUser'] = _order.user.id
        data['orderId'] = _order.orderId
        data['money'] = shopSendIncomesLockeds
        data['moneyLocked'] = shopSendIncomesLockeds
        data['payway'] = payway
        data['changeWay'] = 'add'
        data['orderStatus'] = 'FINISH'
        RecordIncomesDao().create(data)
    # 赠送商户余额
    payway = 'shop_balance'
    if shopUserId:
        shopUser = UserDao().findOneByObjId(shopUserId)
        UserDao().addWallet(payway, shopUser, shop_balance)
        data = {}
        data['scene'] = 'sendForExpense'
        data['shop'] = _order.shop.id
        data['user'] = shopUserId
        data['fromUser'] = _order.user.id
        data['orderId'] = _order.orderId
        data['money'] = shop_balance
        data['payway'] = payway
        data['changeWay'] = 'add'
        data['orderStatus'] = 'FINISH'
        RecordIncomesDao().create(data)

    # 赠送上级购物积分
    payway = 'incomes'
    if _order.user.parent:
        UserDao().addWallet(payway, _order.user.parent, verticalSendIncomes)
        data = {}
        data['scene'] = 'verticalAward'
        data['user'] = _order.user.parent.id
        data['fromUser'] = _order.user.id
        data['orderId'] = _order.orderId
        data['money'] = verticalSendIncomes
        data['payway'] = payway
        data['changeWay'] = 'add'
        data['orderStatus'] = 'FINISH'
        RecordIncomesDao().create(data)

    # 商户推荐人奖励
    payway = 'incomes'
    if shopParentId:
        shopParent = UserDao().findOneByObjId(shopParentId)
        UserDao().addWallet(payway, shopParent, shopParentAward)
        data = {}
        data['scene'] = 'verticalShopAward'
        data['user'] = shopParentId
        data['fromUser'] = _order.user.id
        data['orderId'] = _order.orderId
        data['money'] = shopParentAward
        data['payway'] = payway
        data['changeWay'] = 'add'
        data['orderStatus'] = 'FINISH'
        RecordIncomesDao().create(data)


    # 日释放池
    payway = 'freePoolDay'
    LesseeDao().addWallet(payway, freePoolDays)
    data = {}
    data['scene'] = 'sendForExpense'
    data['user'] = ''
    data['fromUser'] = _order.user.id
    data['orderId'] = _order.orderId
    data['money'] = freePoolDays
    data['payway'] = payway
    data['changeWay'] = 'add'
    data['orderStatus'] = 'FINISH'
    RecordIncomesDao().create(data)
    # 月释放池
    payway = 'freePoolMonth'
    LesseeDao().addWallet(payway, freePoolMonths)
    data = {}
    data['scene'] = 'sendForExpense'
    data['user'] = ''
    data['fromUser'] = _order.user.id
    data['orderId'] = _order.orderId
    data['money'] = freePoolMonths
    data['payway'] = payway
    data['changeWay'] = 'add'
    data['orderStatus'] = 'FINISH'
    RecordIncomesDao().create(data)
    # 运营补贴池
    payway = 'operationAwardPool'
    LesseeDao().addWallet(payway, operationAwards)
    data = {}
    data['scene'] = 'sendForExpense'
    data['user'] = ''
    data['fromUser'] = _order.user.id
    data['orderId'] = _order.orderId
    data['money'] = operationAwards
    data['payway'] = payway
    data['changeWay'] = 'add'
    data['orderStatus'] = 'FINISH'
    RecordIncomesDao().create(data)
    # 助残基金池
    payway = 'helpFundPool'
    LesseeDao().addWallet(payway, helpFunds)
    data = {}
    data['scene'] = 'sendForExpense'
    data['user'] = ''
    data['fromUser'] = _order.user.id
    data['orderId'] = _order.orderId
    data['money'] = helpFunds
    data['payway'] = payway
    data['changeWay'] = 'add'
    data['orderStatus'] = 'FINISH'
    RecordIncomesDao().create(data)
    # 毛利润
    payway = 'grossProfit'
    LesseeDao().addWallet(payway, grossProfits)
    data = {}
    data['scene'] = 'sendForExpense'
    data['user'] = ''
    data['fromUser'] = _order.user.id
    data['orderId'] = _order.orderId
    data['money'] = grossProfits
    data['payway'] = payway
    data['changeWay'] = 'add'
    data['orderStatus'] = 'FINISH'
    RecordIncomesDao().create(data)
    return sendContributionValues